﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Data.SqlClient;
using SiteManager;

public partial class dicountDoctorDentist_discountDentist : System.Web.UI.Page
{
    List<GetDentistBYDentistCalendarDrIdResult> dentisList = new List<GetDentistBYDentistCalendarDrIdResult>();
    protected void Page_Load(object sender, EventArgs e)
        {
        FillGrid();
    }
    private void FillGrid()
    {

        string DentistId = Convert.ToString(Session["StandByDentistId"]);
        if (DentistId.Length > 0)
        {
            DataSet ds;
            ds = CustomRepository.CustumDataSetFunction(returnSql(DentistId), "tbl_drdet");

            if (ds.Tables[0].Rows.Count > 0)
            {
                grdDentist.DataSource = ds.Tables[0].DefaultView;
              
                grdDentist.DataBind();
            }

        }
    }

    public string getPhoto(object photo, object sex)
    {
        if (photo != null)
        {

            string p = photo.ToString();
            if (p.Length > 0)
            {


                if (File.Exists(Request.PhysicalApplicationPath + "\\images\\Dr_images\\" + photo))
                {
                    //string str_path = mgr.getThumbnaleImage(Session["strHttp"].ToString() + "images/Dr_images/" + photo, 80, 100, false, "");

                    //Ian.........
                    string str_path = getThumbnaleImageWithURLFormat(photo.ToString(), 30, 40, false, "", Session["strHTTP"].ToString());
                    return str_path;
                }
                else
                {
                    if (sex.ToString().ToLower() == "m")
                        return "<img height=\"40\" width=\"30\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette-male.gif'>";
                    else
                        return "<img height=\"40\" width=\"30\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette--female.gif'>";
                }
            }
            else
            {
                if (sex.ToString().ToLower() == "m")
                    return "<img height=\"40\" width=\"30\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette-male.gif'>";
                else
                    return "<img height=\"40\" width=\"30\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette--female.gif'>";
            }
        }
        return null;


    }

    public string getThumbnaleImageWithURLFormat(string imgName, int maxWidth, int maxHeight, bool saveImage, string savePath, string baseURL)
    {
        string s;
        double imgHeight, imgWidth;
        //Response.Write(imgName) ;
        s = Server.MapPath("~/images/dr_images/" + imgName);
        //Response.Write("</br>"+s);
        System.Drawing.Image currentImage = System.Drawing.Image.FromFile(s);
        imgHeight = currentImage.Height;
        imgWidth = currentImage.Width;
        if (imgWidth > maxWidth | imgHeight > maxHeight)
        {
            double deltaWidth = imgWidth - maxWidth;
            double deltaHeight = imgHeight - maxHeight;
            double scaleFactor;
            if (deltaHeight > deltaWidth)
                scaleFactor = maxHeight / imgHeight;
            else
                scaleFactor = maxWidth / imgWidth;
            imgWidth *= scaleFactor;
            imgHeight *= scaleFactor;
        }

        if (saveImage)
        {
            if (savePath == "")
                throw new Exception("must pass a file name");
            currentImage.GetThumbnailImage(Convert.ToInt32(imgWidth), Convert.ToInt32(imgHeight), null, IntPtr.Zero).Save(Server.MapPath(savePath));
            currentImage.Dispose();
        }
        //return currentImage.GetThumbnailImage(Convert.ToInt32(imgWidth), Convert.ToInt32(imgHeight), null, IntPtr.Zero);


        return ("<img src=\"" + baseURL + "images/dr_images/" + imgName + "\"" + " height=\"" + imgHeight + "\" width=\"" + imgWidth + "\" border='0' alt=''/>");
    }

    protected void btnBackToSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect(Convert.ToString(Session["strHttps"]) + "dicountDoctorDentist/StandByDoctorSearch.aspx");
    }
    #region Query
    /// <summary>
    /// Inline query has been wirtten because long id string cannot be pass to stored procedure
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    string returnSql(string ids)
    {
        //Response.Write(ids);
        Manager mgr = new Manager(true, "ConnectionString");
        string strSql = @"SELECT QU FROM DENTIST_FUNC_RATING()";

        SqlDataReader dr =CustomRepository.CustumExecutQuery(strSql);
        System.String orderClause = "";
        System.String AttachMiles = "";
        if (Session["Travel_Dis"].ToString() != "" && Session["Travel_Dis"].ToString() != null) //  to order the result first by Distance and then by rating Desc / to Calculate Miles
        {
            orderClause = "order by Distance ASC, Rating Desc";
            AttachMiles = " , (dbo.GetDistanceInMiles('" + Session["ZipCode"].ToString() + "','" + Session["City"].ToString() + "','" + Session["State"].ToString() + "',zipcodesearch)) as Distance";

        }
        else
        {
            orderClause = "order by Rating Desc";
            AttachMiles = ",'' as distance";
        }

        if (dr.Read())
        {
            strSql = @"select d.city, d.zipcode, sp.specialty as specialityName,
            (select tbl_DentistSubSpecialty.specialty from tbl_DentistSubSpecialty where
			 tbl_DentistSubSpecialty.specialty not in(Sp.specialty) and
			 tbl_DentistSubSpecialty.id=d.secondaryspecialityid)
             as SubSpec,
             rtrim((select name from tbl_states where stateId = d.state)) as state,
            CAST((" + dr[0].ToString() + @") as numeric(8,2)) as rating,d.id as id,fname + ' ' +isnull(mname,'') + ' ' + lname as docname, address1 + '<br>' + city + ', ' + state as address,
             Sp.specialty + isnull(', ' + (select tbl_DentistSubSpecialty.specialty from tbl_DentistSubSpecialty where
			tbl_DentistSubSpecialty.specialty not in(Sp.specialty) and
			 tbl_DentistSubSpecialty.id=d.secondaryspecialityid),'') as speciality,
            isnull(dp.website,'')as website,isnull(dp.photo,'') as photo,d.gender" + AttachMiles +
            " from tbl_Dentists d "
                //            + " left outer join tbl_secondaryspeciality s on s.id=d.secondaryspecialityid "
            + " left outer join tbl_DentistSubSpecialty s on s.id=d.secondaryspecialityid "
            + " left outer join tbl_dntpremium dp on dp.dnt_id=d.id "
            + " left outer join tbl_dentistspecialty sp on sp.id=D.specialityid "
             + " where  d.isdelete=0 and d.id in (" + ids + ") " + orderClause;


            //            Response.Write(strSql);

            //a.d. code, will be implemented, if required

            //(dbo.GetDistanceInMiles('" + Session["Travel_Dis"].ToString() + "','" + Session["City"].ToString() 
            //+ @"',substring(d.ZipCode,1,5))) as Distance

        }

        return strSql;
    }
    #endregion
}
